package com.itheima.protocol.socketDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * socket client
 */
public class SocketClient {

    public static void main(String[] args) throws IOException {

        Socket socket = null;
        try{
            socket = new Socket("localhost", 8888);

            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer = new PrintWriter(socket.getOutputStream(),true);

            writer.println("Hello world");


            while (true){
                String serverData = reader.readLine();
                if(serverData == null){
                    break;
                }
                System.out.println("客户端接收到的数据:" + serverData);
            }

            writer.close();
            reader.close();
        }catch (Exception e){

        }finally {

            if(socket != null){
                socket.close();
            }
        }

    }
}
